package com.aaa.ssm.controller;

import com.aaa.ssm.entity.Employee;
import com.aaa.ssm.entity.Permission;
import com.aaa.ssm.servie.EmpService;
import com.aaa.ssm.util.AjaxResult;
import com.aaa.ssm.util.DAOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

@RestController
@Api(tags = "员工信息")
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;


    @Autowired
    HttpSession  session;

    @ApiOperation(value = "获取员工权限和基本信息")
    @GetMapping("getInfo")
    public AjaxResult  getInfo(){

            Employee emp = (Employee) session.getAttribute("emp");
            List<Permission> permisssion = empService.getPermisssion(emp.getRid());
            HashMap<Object, Object> map = new HashMap<>();
            map.put("userInfo",emp);
            map.put("permissionInfo",permisssion);
            return AjaxResult.success(200,"获取成功",map);

    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "账号",
                    required = true, type = "String"),
            @ApiImplicitParam(name = "password", value = "密码",
                    required = true, type = "String"),
    })
    @ApiOperation(value = "员工登录")
    @PostMapping("login")
    public AjaxResult  login(String username,String password){

            Employee login = empService.login(username, password);
            session.setAttribute("emp",login);
            return AjaxResult.success(200,"dengluchenggong");

    }



    @GetMapping
    public AjaxResult  sendCode(String  phone){
         String  ok =  empService.sendCode(phone);
         return AjaxResult.success(200,"ok");
    }
}
